package com.tbvtc.agriculturalcreditchainserver2025.service.impl;

import com.tbvtc.agriculturalcreditchainserver2025.constant.BlockConstant;
import com.tbvtc.agriculturalcreditchainserver2025.dto.ReportSumByTypeDto;
import com.tbvtc.agriculturalcreditchainserver2025.exception.FoodException;
import com.tbvtc.agriculturalcreditchainserver2025.mapper.ConsumerMapper;
import com.tbvtc.agriculturalcreditchainserver2025.properties.ContractAdressProperties;
import com.tbvtc.agriculturalcreditchainserver2025.service.ConsumerService;
import com.tbvtc.agriculturalcreditchainserver2025.service.ITestService;
import com.tbvtc.agriculturalcreditchainserver2025.utils.BlockBaseUtils;
import com.tbvtc.agriculturalcreditchainserver2025.utils.BlockchainUtils;
import com.tbvtc.agriculturalcreditchainserver2025.utils.ResultCodeEnum;
import com.tbvtc.agriculturalcreditchainserver2025.utils.UserType;
import com.tbvtc.agriculturalcreditchainserver2025.utils.redis.CacheClient;
import com.tbvtc.agriculturalcreditchainserver2025.vo.ConsumerVo.CropVo;
import com.tbvtc.agriculturalcreditchainserver2025.vo.ConsumerVo.PlaceOrderVo;
//import com.tbvtc._2025agriculturalcreditchainserver.vo.ConsumerVo.ReportSumByTypeDto;
import org.fisco.bcos.sdk.transaction.model.dto.TransactionResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ConsumerServiceImpl implements ConsumerService {
    @Autowired
    private BlockchainUtils blockchainUtils;
    @Autowired
    private BlockBaseUtils blockBaseUtils;
    @Autowired
    private ContractAdressProperties contractAdressProperties;
    @Autowired
    private ConsumerMapper consumerMapper;
    @Resource
    private CacheClient cacheClient;
    @Resource
    private ITestService testService;

    @Override
    // 获取溯源信息
    public List getTraceInfo(String foodName) throws Exception {
        List<Object> list = List.of(foodName);

        List<Object> info =    info = blockchainUtils.callContract(BlockConstant.ConsumerContractName,
                    "getTraceabilityInfo",
                    contractAdressProperties.getConsumerContractAddress(),
                    list, blockBaseUtils.getPemFilePath(UserType.CONSUMER));
        return info;
    }

    // 获取特定农作物的所有报告
    @Override
    public List getAllReport(String foodName) throws Exception {
        List<Object> list = List.of(foodName);
        List<Object> list1 = blockchainUtils.callContract(BlockConstant.ConsumerContractName, "getCropReports", contractAdressProperties.getConsumerContractAddress(), list, blockBaseUtils.getPemFilePath(UserType.CONSUMER));
        return list1;
    }

    // 获取特定农作物的所有报告摘要
    @Override
    public List getAllReportSummary(String foodName) throws Exception {
        List<Object> list = List.of(foodName);
        List<Object> list1 = blockchainUtils.callContract(BlockConstant.ConsumerContractName,
                "getCropReportsSummary", contractAdressProperties.getConsumerContractAddress(),
                list, blockBaseUtils.getPemFilePath(UserType.CONSUMER));
        return list1;
    }

    // 获取特定类型的报告摘要
    @Override
    public List getReportSummaryByType(ReportSumByTypeDto reportSumByTypeDto) throws Exception {
        List<Object> list = List.of(reportSumByTypeDto.getFoodName(), reportSumByTypeDto.getType());
        List<Object> list1 = blockchainUtils.callContract(BlockConstant.ConsumerContractName,
                "getCropReportsByTypeSummary", contractAdressProperties.getConsumerContractAddress(),
                list, blockBaseUtils.getPemFilePath(UserType.CONSUMER));
        return list1;
    }



    // 获取特定类型的报告摘要
    @Override
    public TransactionResponse confirmDelivery(String orderId, String logisticsInfo) throws Exception {
        List<Object> list = List.of(orderId, logisticsInfo);
        TransactionResponse response = blockchainUtils.sendTransaction("ConsumerContract", "confirmDelivery", contractAdressProperties.getConsumerContractAddress(), list, blockBaseUtils.getPemFilePath(UserType.CONSUMER));
        return response;
    }

    @Override
    public List getOrderDeliveredStatus(String orderId) throws Exception {
        List<Object> list = List.of(orderId);
        List<Object> objects = blockchainUtils.callContract("ConsumerContract", "getOrderDeliveredStatus", contractAdressProperties.getConsumerContractAddress(), list, blockBaseUtils.getPemFilePath(UserType.CONSUMER));
        return objects;
    }

    @Override
    public List getDeliveryCount() throws Exception {
        List<Object> objects = blockchainUtils.callContract("ConsumerContract", "getOrderCount", contractAdressProperties.getConsumerContractAddress(), List.of(), blockBaseUtils.getPemFilePath(UserType.CONSUMER));
        return objects;
    }




}
